
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.ulti.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Product;

/**
 *
 * @author Nero
 */
public class ProductDAO extends AbstractDAO<Product, String> {

    @Override
    protected Class getPOJOClass() {
        return Product.class;
    }

    public static Product getProduct(long id) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Product p WHERE p.isDelete = :IS_DELETE AND p.id = :ID";
        Query query = session.createQuery(hql);
        query.setParameter("IS_DELETE", false);
        query.setParameter("ID", id);
        if (query.list().isEmpty()) {
            session.close();
            throw new Exception("PRODUCT_NOT_EXIST");
        }
        Product product = (Product) query.uniqueResult();
        session.close();
        return product;
    }

    public static List<Product> getProductList() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Product p WHERE p.isDelete = :IS_DELETE ORDER BY p.date desc";
        Query query = session.createQuery(hql);
        query.setParameter("IS_DELETE", false);
        List<Product> productList = query.list();
        session.close();
        return productList;
    }

    public static List<Product> getSame5CategoryProduct(long cat) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Product p WHERE p.isDelete = :IS_DELETE AND p.category.id = :CAT ORDER BY p.viewTime desc";
        Query query = session.createQuery(hql);
        query.setParameter("IS_DELETE", false);
        query.setParameter("CAT", cat);
        List<Product> productList;
        if (query.list().size() < 5) {
            productList = query.list().subList(0, query.list().size());
        } else {
            productList = query.list().subList(0, 5);
        }
        session.close();
        return productList;
    }

    public static List<Product> getProductListForView(long cat) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql;
        Query query;
        if (cat == -1) {
            hql = "FROM Product p WHERE p.isDelete = :IS_DELETE ORDER BY p.viewTime desc";
            query = session.createQuery(hql);
        } else {
            hql = "FROM Product p WHERE p.isDelete = :IS_DELETE AND p.category.id = :CAT ORDER BY p.viewTime desc";
            query = session.createQuery(hql);
            query.setParameter("CAT", cat);
        }
        query.setParameter("IS_DELETE", false);
        List<Product> productList = query.list();
        session.close();
        return productList;
    }

    public static List<Product> getTop9NewProduct() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Product p WHERE p.isDelete = :IS_DELETE ORDER BY p.date desc";
        Query query = session.createQuery(hql);
        query.setParameter("IS_DELETE", false);
        List<Product> productList;
        if (query.list().size() < 9) {
            productList = query.list().subList(0, query.list().size());
        } else {
            productList = query.list().subList(0, 9);
        }
        session.close();
        return productList;
    }

    public static List<Product> getTop9BestsellProduct() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Product p WHERE p.isDelete = :IS_DELETE ORDER BY p.soldTime desc";
        Query query = session.createQuery(hql);
        query.setParameter("IS_DELETE", false);
        List<Product> productList;
        if (query.list().size() < 9) {
            productList = query.list().subList(0, query.list().size());
        } else {
            productList = query.list().subList(0, 9);
        }
        session.close();
        return productList;
    }

    public static void deleteProduct(long id) throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Product p WHERE p.id = :ID";
        Query query = session.createQuery(hql);
        query.setParameter("ID", id);
        if (!query.list().isEmpty()) {
            Product product = (Product) query.uniqueResult();
            if (product.isIsDelete()) {
                session.close();
                throw new Exception("PRODUCT_DELETED");
            } else {
                product.setIsDelete(true);
                session.close();
                updateProduct(product);
            }
        } else {
            session.close();
            throw new Exception("PRODUCT_NOT_EXIST");
        }
    }

    public static void updateProduct(Product product) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = session.beginTransaction();
        try {
            session.saveOrUpdate(product);
            trans.commit();
        } catch (HibernateException ex) {
            trans.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }
}
